package cn.sh.ideal.elasticsearch.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.sh.ideal.elasticsearch.entity.LogEntity;

public class ElasticsearchUtil {

	/**
	 * 处理es查询的结果对象，转化为前端展现对象
	 * @param response
	 * @return
	 */
	public static LogEntity getResultFromSearchResponse(SearchResponse response) {
		List<Map> resultList = new ArrayList<Map>();

		String scrollId = response.getScrollId();

		SearchHit[] hits = response.getHits().getHits();
		for (SearchHit searchHit : hits) {
			Map<String, String> logMess = new HashMap<String, String>();
			//数据再处理
			Map<String, Object> source = searchHit.getSource();
			String time=(String) source.get("@timestamp");
			//处理时间格式问题
			if(time.endsWith("Z")){
				time = time.replace("Z", " UTC");//注意是空格+UTC
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				try {
					time = format.parse(time).toString();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			
			logMess.put("time",time);
			String message=(String) source.get("message");
			if(StringUtils.isEmpty(message)){
				continue;
			}
			try{
				JSONObject messageJson = JSON.parseObject(message);
				logMess.put("message",(String) messageJson.get("message"));
			}catch(Exception e){
				logMess.put("message",message);
			}
			
			resultList.add(logMess);
		}
		return new LogEntity(scrollId, resultList, response.getHits().getTotalHits());
	}
	
	/**
	 * 处理es查询的结果对象,提供日志使用（暂行）
	 * @param response
	 * @return
	 */
	public static LogEntity getStandardResultFromSearchResponse(SearchResponse response) {
		List<Map> resultList = new ArrayList<Map>();

		String scrollId = response.getScrollId();

		SearchHit[] hits = response.getHits().getHits();
		for (SearchHit searchHit : hits) {
			Map<String, String> logMess = new HashMap<String, String>();
			//数据再处理
			Map<String, Object> source = searchHit.getSource();
			String time=(String) source.get("@timestamp");
			//处理时间格式问题
			if(time.endsWith("Z")){
				time = time.replace("Z", " UTC");//注意是空格+UTC
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				try {
					time = format.parse(time).toString();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			
			logMess.put("time",time);
			String message=(String) source.get("message");
			if(StringUtils.isEmpty(message)){
				continue;
			}
			try{
				JSONObject messageJson = JSON.parseObject(message);
				logMess.put("message",(String) messageJson.get("message"));
			}catch(Exception e){
				logMess.put("message",message);
			}
			
			String sourceField = (String) source.get("source");
			logMess.put("source", sourceField);
			
			
			HashMap fields = (HashMap) source.get("fields");
			logMess.put("ce_service", (String) fields.get("ce_service"));
			logMess.put("ce_instance", (String) fields.get("ce_instance"));
			logMess.put("ce_log_type", (String) fields.get("ce_log_type"));
			logMess.put("ce_workload", (String) fields.get("ce_workload"));
			logMess.put("ce_cluster", (String) fields.get("ce_cluster"));
			logMess.put("ce_namespace", (String) fields.get("ce_namespace"));
			
			resultList.add(logMess);
		}
		return new LogEntity(scrollId, resultList, response.getHits().getTotalHits());
	}
	
	
}
